package com.xsf.jieyou.commons;

import com.xsf.jieyou.cloud.api.MessageService;
import com.xsf.jieyou.provider.api.ProviderTbUserLogService;
import com.xsf.jieyou.provider.api.ProviderTbUserService;
import com.xsf.jieyou.provider.domain.TbUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * Description:TODO
 * </p>
 *
 * @author xsf
 * @version v1.0.0
 * @date 2019/11/16 0016 3:25
 * @see com.xsf.jieyou.commons.utils
 **/
@Slf4j
public class MessageLogUtils {
    @Reference(version = "1.0.0")
    private static ProviderTbUserLogService providerTbUserLogService;
    @Reference(version = "1.0.0")
    private static ProviderTbUserService providerTbUserService;
    @Reference(version = "1.0.0")
    private static MessageService messageService;

    /**
     * RocketMQ异步发送用户操作日志
     *
     * @param username 用户名 {@link String}
     * @param request  请求对象 {@link HttpServletRequest}
     */
    public static void sendAdminLoginLog(String username, HttpServletRequest request, String note) {
        TbUser umsAdmin = providerTbUserService.get(username);

        if (umsAdmin != null) {
            /*// 获取请求的用户代理信息
            Browser browser = UserAgentUtils.getBrowser(request);
            String ip = UserAgentUtils.getIpAddr(request);
            String address = UserAgentUtils.getIpInfo(ip).getCity();

            TbUserLog tbUserLog = new TbUserLog();
            tbUserLog.setUserId(umsAdmin.getId());
            tbUserLog.setCreateTime(new Date());
            tbUserLog.setIp(ip);
            tbUserLog.setAddress(address);
            tbUserLog.setUserAgent(browser.getName());
            // 备注
            tbUserLog.setNote(note);

            if (messageService.sendTbUserLog(tbUserLog)) {
                log.info("RocketMQ异步发送用户操作日志成功");
            } else {
                log.warn("RocketMQ异步发送用户操作日志失败");
            }*/
        }

        log.error("RocketMQ异步发送用户操作日志失败");
    }
}
